<script>
import { getQueryString } from '@/utils/optimize.js';
import { setHospitalIdFun } from '@/common/baseUrl.js';
/* eslint-disable no-undef */
export default {
  onLaunch: function (options) {
    // console.log('onLaunch options:', options);

    this.$store.commit('service/setLaunchOptions', options);
    // 小程序版本更新
    this.autoUpdate();

    // 场景值记录
    this.sourceRecord();
  },
  onShow(options) {
    // cid记录
    this.cidRecord(options);
  },
  methods: {
    // 小程序更新
    autoUpdate() {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        console.log('是否有新版本: ', res.hasUpdate);
        if (res.hasUpdate) {
          // 新的版本下载中
          wx.showModal({
            title: '检测到已经有新版本',
            showCancel: false,
            content: '正在下载中，请不要退出!'
          });
        }
      });

      updateManager.onUpdateReady(() => {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启？',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              updateManager.applyUpdate();
            }
          }
        });
      });

      updateManager.onUpdateFailed(function () {
        // 新的版本下载失败
        wx.showModal({
          title: '检测到已经有新版本',
          content: '新版本已经上线啦，请您删除当前小程序，重新搜索打开!'
        });
      });
    },

    // cid记录
    cidRecord(options) {
      if (options.query.q) {
        let q = decodeURIComponent(options.query.q);
        uni.setStorage({
          key: 'cid',
          data: getQueryString(q, 'cid')
        });
        // uni.setStorage({
        //   key: 'getHospitalId',
        //   data: getQueryString(q, 'hospitalId')
        // });
        // 上线前需要把底下这几个 decodeURIComponent 去掉
        console.log(111, decodeURIComponent(q.toString()));
        setHospitalIdFun(getQueryString(decodeURIComponent(q), 'hospitalId'));
        console.log(1212125, decodeURIComponent(q), getQueryString(decodeURIComponent(q), 'hospitalId'));
      } else if (options.query) {
        if (options.query.cid) {
          uni.setStorage({
            key: 'cid',
            data: options.query.cid
          });
        }
        if (options.query.hospitalId) {
          setHospitalIdFun(options.query.hospitalId);
        }
      }
    },

    // 场景值记录
    sourceRecord() {
      const scene = [1011, 1012, 1013, 1047, 1048, 1049, 1017]; // 小程序场景值

      // 获取小程序启动的场景值
      let appSource = wx.getLaunchOptionsSync();

      // 获取打卡参数，下次放到公益项目中 start
      wx.setStorageSync('pbmPunchTheClock', decodeURIComponent('pbmdate%2C2021-09-02%2C10200209'));
      if (appSource.query.scene && appSource.query.scene.indexOf('pbmdate') > -1) {
        wx.setStorageSync('pbmPunchTheClock', decodeURIComponent(appSource.query.scene));
      }
      // 获取打卡参数，下次放到公益项目中 end

      // 通过二维码进入场景值1011 1012 1013
      uni.setStorageSync('appSourceQuery', appSource.query);
      // 如果用户进入的是全部服务页，且是扫二维码或者小程序码进来的，那么我们断定，用户是扫码冰箱贴进来的。

      const sourceBol = scene.includes(Number(appSource.scene)); // 判断小程序来源是否为扫码
      if (appSource.path === 'pages/service/index' && sourceBol) {
        uni.setStorageSync('appSource', 'scanFridgeQr');
      }
      // 公众号场景值  1035 公众号自定义菜单
      const officialAccountScene = [1035];
      if (officialAccountScene.includes(Number(appSource.scene))) {
        uni.setStorageSync('appSource', 'officialAccount');
      }
    }
  }
};
</script>

<style lang="scss">
@font-face {
  font-family: 'gg-iconfont'; /* Project id 2556251 */
  src: url('//at.alicdn.com/t/font_2556251_p759at2tovh.woff2?t=1621332212513') format('woff2'),
    url('//at.alicdn.com/t/font_2556251_p759at2tovh.woff?t=1621332212513') format('woff'),
    url('//at.alicdn.com/t/font_2556251_p759at2tovh.ttf?t=1621332212513') format('truetype');
}
page {
  background: $uni-bg-color;
}
$msg: '暂无' !default;
@mixin no-content($msg) {
  width: auto;
  height: 320rpx;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 240rpx 240rpx;
  position: relative;
  margin: 100rpx 0;
  &::after {
    content: $msg;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    font-size: 36rpx;
    text-align: center;
    color: #999999;
  }
}

.zwbg {
  @include no-content('暂无报告');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwbg.png');
}
.zwjl {
  @include no-content('暂无记录');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwbg.png');
}
.zwda {
  @include no-content('暂无档案');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwbg.png');
}
.zwsj {
  @include no-content('暂无数据');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwsj.png');
}
.zwxx {
  @include no-content('暂无信息');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwxx.png');
}
.zwtp {
  @include no-content('暂无图片');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwtp.png');
}
.zwwz {
  @include no-content('暂无位置');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwwz.png');
}
.zwgw {
  @include no-content('暂无购物');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwgw.png');
}
.zzjz {
  @include no-content('正在加载');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zzjz.png');
}
.zwwl {
  @include no-content('暂无网络');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwwl.png');
}
.zwmsg {
  @include no-content('暂无消息');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwxx2.png');
}
.ssbd {
  @include no-content('搜索不到');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/ssbd.png');
}
.ssbd-default {
  @include no-content('请输入药品名称进行搜索');
  background-image: url('https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/ssbd.png');
}
</style>
